package com.study.config;

import com.study.common.util.EncryptionUtil;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 加密数据源
 * @author: huangw
 * @create: 2019-11-05
 */
@Configuration
public class DataSourceConfig {

    @Value("${dataInfo.url}")
    private String url;

    @Value("${dataInfo.driverClassName}")
    private String driverClassName;

    @Value("${dataInfo.username}")
    private String username;

    @Value("${dataInfo.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setDriverClassName(driverClassName);
        hikariDataSource.setJdbcUrl(url);
        hikariDataSource.setUsername(EncryptionUtil.decrypt(username));
        hikariDataSource.setPassword(EncryptionUtil.decrypt(password));
        return hikariDataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }

}
